package des;
//Encrypt file

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;


public class Encryptfile 
{
	private Cipher c;
    private String filepath;
    private static String path;
    private String path2;
    CipherInputStream cis;
    
    //Constructor
    public Encryptfile()
    {
    	super();
    }
    
    //set file name for encryption
    public void setFileName(String filepath)
    {
    	this.filepath=filepath;
    }
	
	//encrypt the file
    public void encrypt(Key k) throws Exception
    {
    	path=this.filepath;
    	c = Cipher.getInstance("DES");
    	//initialize the mode
    	c.init(Cipher.ENCRYPT_MODE,k);
    	//pass the file through cipherinputstream
    	cis = new CipherInputStream(new FileInputStream(new File(path)),c);
    	path2="sample123.au";
    	File ftarget=new File(path2);
    	ftarget.createNewFile();
    	FileOutputStream fos1 = new FileOutputStream(path2);
    	int j;
    	while((j=cis.read())!=-1)
    	{
    		//write the encrypted data onto the file
    		fos1.write(j);
    	}
    	//close the fileoutputstream
		fos1.close();
	}
	
    //get the name of file to be encrypted
	public String getfilename()
	{
		return path2;
	}
}
